package vg.services.user_interface_manager.realization;

import java.awt.BorderLayout;
import java.util.Observable;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import vg.services.user_interface_manager.interfaces.AUserInterfaceElement;
import vg.services.user_interface_manager.interfaces.IUserInterfacePanel;

public class UserInterfacePanel extends AUserInterfaceElement implements IUserInterfacePanel {
	private JPanel      view;
	private JTabbedPane tabs;
	private JComponent  mainTab = null;
	//-------------------------------------------------------------------------
	public UserInterfacePanel(String title, Icon icon) {
		super(title, icon);
		this.view = new JPanel(new BorderLayout());
		this.tabs = new JTabbedPane();
		this.view.add(this.tabs, BorderLayout.CENTER);
	}
//-----------------------------------------------------------------------------
//Implementation of IUserInterfacePanel.
//-----------------------------------------------------------------------------
	public void setMainTab(AUserInterfaceElement element) {
		if(this.mainTab==null) {
			this.view.remove(this.tabs);
		} else {
			this.view.remove(this.mainTab);
		}
		this.mainTab = element.getView();
		this.view.add(this.mainTab, BorderLayout.CENTER);
		this.view.updateUI();
	}
	public void setMainTab(String title, JComponent element, Icon icon) {
		if(this.mainTab==null) {
			this.view.remove(this.tabs);
		} else {
			this.view.remove(this.mainTab);
		}
		this.mainTab = element;
		this.view.add(this.mainTab, BorderLayout.CENTER);
		this.view.updateUI();		
	}
	public void addTab(AUserInterfaceElement element) {
		this.tabs.add(_title, element.getView());
	}
	public void addTab(String title, JComponent element, Icon icon) {
		this.tabs.add(title,element);
	}
	public void closeCurrTab() {
		if(this.mainTab!=null) {
			this.view.remove(this.mainTab);
			this.mainTab = null;
			this.view.add(this.tabs, BorderLayout.CENTER);
		} else {
			int index;
			
			index = this.tabs.getSelectedIndex();
			if(index<0) return;
			this.tabs.remove(index);
		}
		this.view.updateUI();
	}
//-----------------------------------------------------------------------------
//Implementation of AUserInterfaceElement.
//-----------------------------------------------------------------------------
	public JComponent getView() {
		return(this.view);
	}
	public void update(Observable o, Object arg) {
		
	}
}
